package mobile.game.common.net;
                    
import mobile.game.core.CallPoint;
import mobile.game.core.Port;
import mobile.game.core.config.JowDistr;
import mobile.game.core.support.log.LogCore;
import mobile.game.core.support.Param;
import mobile.game.core.support.Utils;
import mobile.game.core.support.function.*;
import mobile.game.core.gen.proxy.RPCProxyBase;
import java.util.List;
import mobile.game.core.Chunk;
import mobile.game.core.gen.JowGenFile;


@SuppressWarnings("all")
@JowGenFile
public final class ConnServiceProxy extends RPCProxyBase {
	public final class EnumCall{
		public static final int MOBILE_GAME_COMMON_NET_CONNSERVICE_CHECK_LONG = 1;
		public static final int MOBILE_GAME_COMMON_NET_CONNSERVICE_SENDMSG_LIST_INT_CHUNK = 2;
		public static final int MOBILE_GAME_COMMON_NET_CONNSERVICE_SENDMSG_LIST_LIST_LIST = 3;
		public static final int MOBILE_GAME_COMMON_NET_CONNSERVICE_SENDMSGTOALL_INT_CHUNK = 4;
	}
	private static final String SERV_ID = "conn";
	
	private CallPoint remote;
	private Port localPort;
	private String callerInfo;
	
	/**
	 * 私有构造函数
	 * 防止实例被私自创建 必须通过newInstance函数
	 */
	private ConnServiceProxy() {}

	/**
	 * 获取实例
	 * 大多数情况下可用此函数获取
	 * @param localPort
	 * @return
	 */
	public static ConnServiceProxy newInstance() {
		String portId = JowDistr.getPortId(SERV_ID);
		if(portId == null) {
			LogCore.remote.error("通过servId未能找到上级Port: servId={}", SERV_ID);
			return null;
		}
	
		String nodeId = JowDistr.getNodeId(portId);
		if(nodeId == null) {
			LogCore.remote.error("通过portId未能找到上级Node: portId={}", portId);
			return null;
		}
	
		return createInstance(nodeId, portId, SERV_ID);
	}
	
	
	/**
	 * 创建实例
	 * @param localPort
	 * @param node
	 * @param port
	 * @param id
	 * @return
	 */
	private static ConnServiceProxy createInstance(String node, String port, Object id) {
		ConnServiceProxy inst = new ConnServiceProxy();
		inst.localPort = Port.getCurrent();
		inst.remote = new CallPoint(node, port, id);
		
		return inst;
	}
	
	/**
	 * 监听返回值
	 * @param obj
	 * @param methodName
	 * @param context
	 */
	public void listenResult(JowFunction2<Param, Param> method, Object...context) {
		listenResult(method, new Param(context));
	}
	
	/**
	 * 监听返回值
	 * @param obj
	 * @param methodName
	 * @param context
	 */
	public void listenResult(JowFunction2<Param, Param> method, Param context) {
		context.put("_callerInfo", callerInfo);
		localPort.listenResult(method, context);
	}
	
	
	public void listenResult(JowFunction3<Boolean, Param, Param> method, Object...context) {
		listenResult(method, new Param(context));
	}
	
	public void listenResult(JowFunction3<Boolean, Param, Param> method, Param context) {
		context.put("_callerInfo", callerInfo);
		localPort.listenResult(method, context);
	}
	
	
	/**
	 * 等待返回值
	 */
	public Param waitForResult() {
		return localPort.waitForResult();
	}
	
	public void check(long connId) {
		callerInfo = Utils.getCallerInfo();
		localPort.call(remote, EnumCall.MOBILE_GAME_COMMON_NET_CONNSERVICE_CHECK_LONG, new Object[]{ connId });
	}
	
	public void sendMsg(List connIds, int msgId, Chunk msgbuf) {
		callerInfo = Utils.getCallerInfo();
		localPort.call(remote, EnumCall.MOBILE_GAME_COMMON_NET_CONNSERVICE_SENDMSG_LIST_INT_CHUNK, new Object[]{ connIds, msgId, msgbuf });
	}
	
	public void sendMsg(List connIds, List idList, List chunkList) {
		callerInfo = Utils.getCallerInfo();
		localPort.call(remote, EnumCall.MOBILE_GAME_COMMON_NET_CONNSERVICE_SENDMSG_LIST_LIST_LIST, new Object[]{ connIds, idList, chunkList });
	}
	
	public void sendMsgToAll(int msgId, Chunk msgbuf) {
		callerInfo = Utils.getCallerInfo();
		localPort.call(remote, EnumCall.MOBILE_GAME_COMMON_NET_CONNSERVICE_SENDMSGTOALL_INT_CHUNK, new Object[]{ msgId, msgbuf });
	}
}